//可否下拉
var is_bool=true;
// 页码信息
var page_no=1;
//搜索条件
var code='';
var series = '';
var type = '';
// 下拉框对象
var hgs1 = {};
var hgs2 = {};

$(function() {
    getProductList()
    $('#icon-remove-series').click(function() {
    	series = '';
        $('.product').html('');
        page_no=1;
        $('#series').html('产品系列');
        getProductList();
    });
    $('#icon-remove-type').click(function() {
    	type = '';
        $('.product').html('');
        page_no=1;
        $('#type').html('品类名称');
        getProductList();
    });
    $('.icon-remove-code').click(function(){
        code='';
        $('.product').html('');
        page_no=1;
        $(this).prev().val('');
        getProductList();
    });

    $('.code').blur(function(){
        code=$(this).val();
    });

    $('.search-button').click(function(){
        page_no=1;
        $('.product').html('');
        getProductList();
    });
    $('#series').click(function() {
        chooseSeriesParam();
    });
    $('#type').click(function() {
        chooseTypeParam();
    });


    $(window).scroll(function(){
    　　var scrollTop = $(this).scrollTop();
    　　var scrollHeight = $(document).height();
    　　var windowHeight = $(this).height();
    　　if(scrollTop + windowHeight == scrollHeight&&is_bool){
    　　　　getProductList();
    　　}
    });
})



function getProductList() {
    //禁止滚动
    is_bool=false;
    $('.product').show();
    //搜索结果
    $('.search-result lable').eq(0).html('?');
    var search_array=[];
    if(code!=''){
        search_array.push(code);
    }
    if(series!=''){
        search_array.push(series);
    }
    if(type!=''){
        search_array.push(type);
    }
    $('.search-result lable').eq(1).html(search_array.join('，'));
    $('.search-result').show();
    $('.load').show();
    $.ajax({
        type: "post",
        url: "Api/Product/getProductList",
        data: {
            park_id: $('#park_id').val(),
            openid:$('#openid').val(),
            code:code,
            series:series,
            type:type,
            page_no:page_no
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                setTimeout(function(){
                    $('.load').hide();
                    var t = JSON.parse(data.v);
                    $('.search-result lable').eq(0).html(t.count);
                    $.each(t.product, function(k, v) {
                        // alert(v.img[0]);
                        $('.product-demo a').attr('href',getRootPath()+'/Home/Product/product?park_id='+$('#park_id').val()+'&product='+v.id);
                        if(v.img[0]){
                           $('.product-demo .product-img img').attr('src',v.img[0]).attr('data-src',v.img[0]); 
                        }
                        else{
                            $('.product-demo .product-img img').attr('src','public/home/images/no-img.jpg').attr('data-src','public/home/images/no-img.jpg'); 
                        }
                        
                        $('.product-demo .product-num').html(sub_str(v.code,7));
                        $('.product-demo .product-collection lable').html(v.collect_num);
                        if(v.is_collect){
                            $('.product-demo .product-collection i').attr('class','fa fa-heart');
                        }
                        else{
                            $('.product-demo .product-collection i').attr('class','fa fa-heart-o');
                        }   
                        $('.product-demo .product-line-1').html(v.type);
                        $('.product-demo .product-line-2').html(v.series+'系列');
                        $('.product-demo .product-line-3 lable').html(v.long+'×'+v.wide+'×'+v.high);
                        $('.product').append($('.product-demo').html());
                    });
                    is_bool=t.have;
                    page_no++;
                },1000);
                lazyLoad();
                $(window).scroll();
            } else {

            }
        }
    });
}


function chooseSeriesParam() {
    $.ajax({
        type: "post",
        url: "Api/Product/getSeriesData",
        data: {
        	park_id: $('#park_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                hgs1 = new selectSwiper({
                    el: '.select-box1',
                    mustSelect:true, // 是否必选，默认false
                    data: t,
                    init:function(index){},
                    okFunUndefind: function() { //选择空执行
                        // errorAlert('请选择');
                        // return false;
                        series='';
                        page_no=1;
                        $('.product').html('');
                        $('#series').html('全部');
                        getProductList();
                        hgs1.closeSelectSwiper();
                    },
                    okFun: function(index) { //确认按钮执行
                        series=this.data[index];
                        page_no=1;
                        $('.product').html('');
                        $('#series').html(series);
                        getProductList();
                    }
                });
                hgs1.openSelectSwiper();
            } else {
            	errorAlert(data.msg);
            }
        }
    });
}

function chooseTypeParam() {
    $.ajax({
        type: "post",
        url: "Api/Product/getTypeData",
        data: {
            park_id: $('#park_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                hgs2 = new selectSwiper({
                    el: '.select-box2',
                    mustSelect:true, // 是否必选，默认false
                    data: t,
                    init:function(index){},
                    okFunUndefind: function() { //选择空执行
                        type='';
                        page_no=1;
                        $('.product').html('');
                        $('#type').html('全部');
                        getProductList();
                        hgs2.closeSelectSwiper();
                    },
                    okFun: function(index) { //确认按钮执行
                        type=this.data[index];
                        page_no=1;
                        $('.product').html('');
                        $('#type').html(type);
                        getProductList();
                    }
                });
                hgs2.openSelectSwiper();
            } else {
                errorAlert(data.msg);
            }
        }
    });
}